﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.Data;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class SpecimensDaoImpl : BaseGenericDaoImpl, ISpecimensDao
    {
        public bool DeleteObject(Object entity)
        {
            throw new NotImplementedException();
        }
        public bool saveSpecimens(ObservableCollection<SpecInfo> specList)
        {
            bool isSaved = true;

            SpecInfo spec;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < specList.Count; i++)
            {
                spec = specList[i];

                sb.Clear();
                sb.Append(" insert into");
                sb.Append(" DUO_SPECIMENS (SPECID, NAME, PATIENTID, COLLECTTIME, COLLECTER, SPECIFICATION, SPECTYPE, MAXFREEZETHRAWTIMES, SUBJECT, SPECSOURCE)");
                sb.Append(" values(");
                sb.Append(" '" + spec.Id + "',");
                sb.Append(" '" + spec.Name + "',");
                sb.Append(" '" + spec.PatientId + "',");
                sb.Append("to_date ( '" + spec.FollowUpDate.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' )" + ",");
                sb.Append(" '" + spec.Collecter + "',");
                sb.Append(" '" + spec.Specification + "',");
                sb.Append(" '" + spec.SpecType + "',");
                sb.Append(" " + spec.MaxFreezeThrawTimes + ",");
                sb.Append(" '" + spec.Subject+"',");
                sb.Append(" '" + spec.SpecSource + "'");
                sb.Append(")");
               bool success = this.dbConn.Insert(sb.ToString());
               isSaved = isSaved & success;
                //保存标本与容器的关联关系
              
            }           
            return isSaved;
        }

        public bool SaveSpecContainer(ObservableCollection<SiteSelectEntity> entityList)
        {
            bool isSaved = true; 
           
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < entityList.Count; j++)
            {
                sb.Append("select * from  DUO_CONTAINER  where SITECODE='" + entityList[j].SiteCode + "'");
                DataTable dt = ConnectDatabase.getInstance().Select(sb.ToString());
                if (dt.Rows.Count > 0 && dt != null)
                {
                    sb.Clear();
                    sb.Append("update DUO_CONTAINER set ISSTORED = 'True', STOREDDATE='" + DateTime.Now.ToFileTime().ToString());
                    sb.Append("' where SITECODE='" + entityList[j].SiteCode + "'");
                    dt = ConnectDatabase.getInstance().Select(sb.ToString());
                }
                else
                {
                   /* sb.Clear();
                    sb.Append("insert into DUO_CONTAINER (CONTAINERID,SITECODE,MODELTYPE,REMAININGCAPACITY,WHOLECAPACITY,DESCRIPTION,METATYPE,ISSTORED,STOREDDATE) values");
                    sb.Append("('" + entityList[j].SpecId + "', '" + entityList[j].SiteCode + "','','1','1','','','True',");
                    sb.Append("to_timestamp('" + DateTime.Now + "','yyyy-mm-dd hh24:mi:ss.ff'))");
                    dt = ConnectDatabase.getInstance().Select(sb.ToString());
                    */
                    isSaved = false;
                }
            }
            return isSaved;
        }

        public bool SaveSpecEntities(List<SpecEntity> entityList)
        {
            bool success = true;
            SpecEntity entity;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < entityList.Count; i++)
            {
                entity = entityList[i];
                bool isSaved = SaveSpecEntity(entity);
                success = success & isSaved;
            }
            return success;
        }
        public bool SaveSpecEntity(SpecEntity entity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append("  DUO_SPEC_ENTITY (ENTITYID, SPECID, SITECODE, ENTITYSTATE, FREEZETHRAWTIMES, STOREDDATE, CUSTOMCODE)");
            sb.Append(" values(");
            sb.Append(" '" + entity.Id + "',");
            sb.Append(" '" + entity.SpecId + "',");
            sb.Append(" '" + entity.SiteCode + "',");
            sb.Append(" '" + entity.EntityState.ToString() + "',");
            sb.Append(" " + entity.FreezeThrawTimes.ToString() + ",");
            sb.Append("to_date ( '" + entity.StoredDate.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ), ");
            sb.Append(" '" + entity.CustomCode + "'");
            sb.Append(")");
            return this.dbConn.Insert(sb.ToString());
        }

        public List<SpecInfo> loadAllSpecimens()
        {
            throw new NotImplementedException();
        }

        public List<SpecInfo> loadSpecimensById(string specId)
        {
            throw new NotImplementedException();
        }

        public List<SpecInfo> loadSpecimensByName(string specName)
        {
            throw new NotImplementedException();
        }
        public bool SaveOrUpdateObject(Object entity)
        {
            throw new NotImplementedException();
        }

        public void SetSpecState(List<SpecEntity> specQueryResults, string outWay)
        {
            if (specQueryResults == null)
                return;
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < specQueryResults.Count; j++)
            {
                sb.Clear();
                sb.Append(" update duo_spec_entity");
                sb.Append(" set entitystate ='" + outWay + "'");
                sb.Append(" where entityid = '" + specQueryResults[j].Id + "'");
               
                this.dbConn.Insert(sb.ToString());
            }              
        }

        public List<SpecEntity> GetAllSpecEntities()
        {
            SpecEntity specEntity = null;
            List<SpecEntity> specEntityList = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select * from duo_spec_entity");

            DataTable dt =  this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                specEntityList = new List<SpecEntity>();
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    specEntity = new SpecEntity();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        specEntity.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    specEntityList.Add(specEntity);
                }
                return specEntityList;
            }
            else
                return null;
        }
    }
}
